package com.atguigu.lease.web.app.service.impl;

import com.atguigu.lease.common.context.LoginUser;
import com.atguigu.lease.common.context.LoginUserContext;
import com.atguigu.lease.model.entity.*;
import com.atguigu.lease.model.enums.ItemType;
import com.atguigu.lease.model.enums.LeaseStatus;
import com.atguigu.lease.model.enums.ReleaseStatus;
import com.atguigu.lease.web.app.mapper.*;
import com.atguigu.lease.web.app.service.*;
import com.atguigu.lease.web.app.vo.apartment.ApartmentItemVo;
import com.atguigu.lease.web.app.vo.attr.AttrValueVo;
import com.atguigu.lease.web.app.vo.fee.FeeValueVo;
import com.atguigu.lease.web.app.vo.graph.GraphVo;
import com.atguigu.lease.web.app.vo.history.HistoryItemVo;
import com.atguigu.lease.web.app.vo.room.RoomDetailVo;
import com.atguigu.lease.web.app.vo.room.RoomItemVo;
import com.atguigu.lease.web.app.vo.room.RoomQueryVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author liubo
 * @description 针对表【room_info(房间信息表)】的数据库操作Service实现
 * @createDate 2023-07-26 11:12:39
 */
@Service
@Slf4j
public class RoomInfoServiceImpl extends ServiceImpl<RoomInfoMapper, RoomInfo>
        implements RoomInfoService {

    @Autowired
    RoomInfoMapper roomInfoMapper;

    @Autowired
    ApartmentInfoService apartmentInfoService;

    @Autowired
    GraphInfoService graphInfoService;

    @Autowired
    AttrValueService attrValueService;

    @Autowired
    FacilityInfoService facilityInfoService;

    @Autowired
    LabelInfoService labelInfoService;

    @Autowired
    PaymentTypeService paymentTypeService;

    @Autowired
    FeeValueService feeValueService;

    @Autowired
    LeaseTermService leaseTermService;

    @Autowired
    LeaseAgreementService leaseAgreementService;

    @Autowired
    BrowsingHistoryMapper browsingHistoryMapper;

    @Override
    public IPage<RoomItemVo> listPageRoomItemVo(IPage<RoomItemVo> page, RoomQueryVo queryVo) {

        return roomInfoMapper.listPageRoomItemVo(page,queryVo);
    }

    @Override
    public RoomDetailVo getDetailById(Long id) {
        RoomDetailVo roomDetailVo = new RoomDetailVo();

        //查房间信息
        RoomInfo roomInfo = super.getById(id);
        Boolean b = roomInfo.getIsDeleted()==1;
        roomDetailVo.setIsDelete(b);
        BeanUtils.copyProperties(roomInfo,roomDetailVo);


        //查App端公寓信息

        ApartmentItemVo apartmentItemVo = new ApartmentItemVo();
        BeanUtils.copyProperties(apartmentInfoService.getById(roomInfo.getApartmentId()),apartmentItemVo);
        //查出来的对应的Lable放进 apartmentItemVo
        List<LabelInfo> labelInfoList = labelInfoService.listByapid(apartmentItemVo.getId());
        apartmentItemVo.setLabelInfoList(labelInfoList);

        //查出来对应的图片信息 放进去
        LambdaQueryWrapper<GraphInfo> lambdaQueryWrapperG = new LambdaQueryWrapper<>();
        lambdaQueryWrapperG.eq(GraphInfo::getItemType,ItemType.APARTMENT);
        lambdaQueryWrapperG.eq(GraphInfo::getItemId,apartmentItemVo.getId());
        graphInfoService.list();
        //对应公寓的最低价房间的价格
        BigDecimal minRent= apartmentInfoService.getMinRent(apartmentItemVo.getId());
        apartmentItemVo.setMinRent(minRent);

        roomDetailVo.setApartmentItemVo(apartmentItemVo);

        //查询房间对应的图片列表
        LambdaQueryWrapper<GraphInfo> lambdaQueryWrapperRoomGra = new LambdaQueryWrapper<>();
        lambdaQueryWrapperRoomGra.eq(GraphInfo::getItemType,ItemType.ROOM);
        lambdaQueryWrapperRoomGra.eq(GraphInfo::getItemId,roomInfo.getId());
        List<GraphInfo> listG = graphInfoService.list(lambdaQueryWrapperG);
        List<GraphVo> graphVos = new ArrayList<>();
        for (GraphInfo graphInfo : listG) {
            GraphVo graphVo = new GraphVo();
            graphVo.setName(graphInfo.getName());
            graphVo.setUrl(graphInfo.getUrl());
            graphVos.add(graphVo);
        }

        roomDetailVo.setGraphVoList(graphVos);

        //查询属性信息列表
        List<AttrValueVo> listAtvo = attrValueService.listByRoomId(roomInfo.getId());
        roomDetailVo.setAttrValueVoList(listAtvo);

        //查询配套信息列表
       List<FacilityInfo> listFac = facilityInfoService.listByRoomId(roomInfo.getId());
       roomDetailVo.setFacilityInfoList(listFac);

       //查询房间对应标签信息
        List<LabelInfo> listLa = labelInfoService.listByRoomId(roomInfo.getId());
        roomDetailVo.setLabelInfoList(listLa);

        //查询房间对应支付方式列表
        List<PaymentType> listPay = paymentTypeService.listByRoomId(roomInfo.getId());
        roomDetailVo.setPaymentTypeList(listPay);

        //杂费列表
        List<FeeValueVo> feeValueVoList = feeValueService.listByroomId(apartmentItemVo.getId());
        roomDetailVo.setFeeValueVoList(feeValueVoList);

        //租期列表
        List<LeaseTerm> leaseTermList = leaseTermService.listByRoomId(roomInfo.getId());
        roomDetailVo.setLeaseTermList(leaseTermList);

        //
        LambdaQueryWrapper<LeaseAgreement> lambdaQueryWrapperleaseAgr = new LambdaQueryWrapper<>();
        lambdaQueryWrapperleaseAgr.in(LeaseAgreement::getStatus,LeaseStatus.SIGNED,LeaseStatus.WITHDRAWING);
        lambdaQueryWrapperleaseAgr.eq(LeaseAgreement::getRoomId,roomInfo.getId());
        LeaseAgreement one = leaseAgreementService.getOne(lambdaQueryWrapperleaseAgr);
        roomDetailVo.setIsCheckIn(one==null);


      //===================================================保存到历史记录
        //============================================================将查询到的信息添加到历史记录



        LoginUser loginUser = LoginUserContext.getLoginUser();
        BrowsingHistory browsingHistory = new BrowsingHistory();
        browsingHistory.setUserId(loginUser.getUserId());
        browsingHistory.setRoomId(roomInfo.getId());
        browsingHistory.setBrowseTime(new Date());
        browsingHistoryMapper.insert(browsingHistory);



        return roomDetailVo;
    }

    @Override
    public IPage<RoomItemVo> listByidVo(IPage<RoomItemVo> page, Long id) {
        return roomInfoMapper.listPageByApartId(page,id);
    }

    @Override
    public BigDecimal minRentByApId(Long id) {
        return roomInfoMapper.minRentByApId(id);

    }
}




